<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>检测任务管理</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}" media="screen"/>
    <link rel="stylesheet" th:href="@{/static/css/layui.css}" media="all"/>
</head>
<body>
<form class="layui-form">
    <div class="layui-panel">
        <div style="margin: 15px;">
            <div class="layui-form-item">
                <label class="layui-form-label">名称:</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" placeholder="请输入名称" class="layui-input"
                           lay-affix="clear">
                </div>
                <label class="layui-form-label">作业数据源:</label>
                <div class="layui-input-inline">
                    <input type="text" name="dataBaseName" id="dataBaseName" placeholder="请输入作业数据源"
                           class="layui-input" lay-affix="clear">
                </div>
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-inline">
                    <select name="state" lay-filter="state">
                        <option value=""></option>
                        <option value="开始监控">开始监控</option>
                        <option value="停止监控">停止监控</option>
                    </select>
                </div>
                <div class="layui-input-normal">
                    <button class="layui-btn layui-btn-primary layui-border-green search-btn" lay-submit
                            lay-filter="table-search">
                        <i class="layui-icon layui-icon-search"> </i>查 询
                    </button>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-panel" style="margin-top: 15px;">
        <div class="layui-form nowrap">
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</form>
</body>
<script th:src="@{/static/layui.js}"></script>
<script th:src="@{/static/base64.min.js}"></script>
<script th:replace="~{base::ctx}"/>
<script type="text/html" id="toolbarTable">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="view">SQL</a>
        <a class="layui-btn layui-bg-blue layui-btn-xs" lay-event="updateCron">CRON</a>
        <a class="layui-btn layui-bg-purple layui-btn-xs" lay-event="updateState">{{= d.state=='停止监控'?
            '开始':'停止'}}</a>
        <a class="layui-btn layui-bg-red layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>
<script>
    layui.use(function () {
        const layer = layui.layer;
        const form = layui.form;
        const $ = layui.jquery;
        const table = layui.table;
        const laytpl = layui.laytpl;
        const util = layui.util;

        table.render({
            elem: '#dataTable',
            url: ctx + 'detectionManager/list',
            toolbar: true,
            defaultToolbar: ['filter', 'exports', 'print'],
            page: true,
            height: '525',
            cols: [
                [
                    {field: 'id', title: 'id', sort: true, fixed: true},
                    {
                        field: 'name', title: '名称', sort: true, fixed: true, templet: function (d) {
                            return '<a href="/detectionManager/reportPage?id=' + d.id + '" class="layui-table-link">' + d.name + '</a>'
                        }
                    },
                    {field: 'dataBaseName', title: '作业数据源', sort: true, fixed: true},
                    {field: 'expression', title: '告警表达式', sort: true, fixed: true},
                    {field: 'cron', title: 'CRON表达式', sort: true, fixed: true, width: 200},
                    {field: 'alarmLevel', title: '告警级别', sort: true, fixed: true},
                    {field: 'messageName', title: '告警模板', sort: true, fixed: true},
                    {field: 'state', title: '状态', sort: true, fixed: true},
                    {field: 'createUser', title: '创建人', sort: true, fixed: true},
                    {field: 'createTime', title: '创建时间', width: 200, sort: true, fixed: true},
                    {fixed: 'right', title: '操作', width: 270, toolbar: '#toolbarTable'}
                ]
            ],
            parseData: res => parseData(res)
        });

        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'view':
                    let str = Base64.decode(data.sqlContent) || "";
                    const content = laytpl('<div style="margin: 5px;"><pre class="layui-code">{{= d.content}}</pre></div>')
                        .render({content: str});
                    openContent("查看作业SQL内容", content);
                    break;
                case 'updateCron':
                    layer.prompt({
                        formType: 2,
                        value: data.cron,
                        anim: 'slideLeft',
                        title: '请输入Cron表达式',
                        area: ['300px', '200px']
                    }, function (value, index) {
                        layer.confirm("确认修改CRON？", function () {
                            post(ctx + "detectionManager/executeOnce/3/" + data.id + "?cron=" + value, null, (res) => {
                                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                                    $(".search-btn").click();
                                    layer.closeAll();
                                });
                            })
                        });
                    });
                    break;
                case 'updateState':
                    var path = data.state === '开始监控' ? '1' : '2';
                    var message = data.state === '开始监控' ? '确认要停止监控吗?' : '确认要开始监控吗?';
                    layer.confirm(message, function () {
                        post(ctx + "detectionManager/executeOnce/" + path + "/" + data.id, null, (res) => {
                            layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                                $(".search-btn").click();
                            });
                        })
                    })
                    break;
                case 'delete':
                    layer.confirm("您确认要删除此任务吗？", function () {
                        post(ctx + "detectionManager/executeOnce/4/" + data.id, null, (res) => {
                            layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                                $(".search-btn").click();
                            });
                        })
                    })
                    break;
            }
        });

        form.on('submit(table-search)', function (data) {
            const field = data.field;
            table.reloadData('dataTable', {
                scrollPos: true,
                where: field
            });
            return false;
        });
    });
</script>
</html>